package com.lanou.moving.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.moving.R;
import com.lanou.moving.activity.CollectionSongListActivity;
import com.lanou.moving.activity.LocalMusicActivity;
import com.lanou.moving.activity.LoginActivity;
import com.lanou.moving.activity.MusicDownloadActivity;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.utils.MyConfig;
import com.lanou.moving.utils.PreferencesUtils;
import com.lanou.moving.view.PersonalScrollView;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;

import de.hdodenhof.circleimageview.CircleImageView;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/20.
 */
public class MineFragment extends BaseFragment implements View.OnClickListener {

    public static final String ARG_POSITION = "position";

    private PersonalScrollView personalScrollView;// 自定义的ScrollView
    private ImageView image_bg;// 背景图片
    private CircleImageView headpicture;// 头像
    private TextView mine_login;// 登录
    private View line;// 头像上的线


    private UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");
    private ImageLoader imageLoader;

//    // 变换头像的图片
//    private int mipmap_id[] = {R.mipmap.default_user_hole,
//            R.mipmap.default_artist_hole};
//
//    private int current_id;// 判断当前头像是哪一个图片

    private RelativeLayout mine_local_music;//本地音乐
    private TextView mine_local_songs, mine_collections, mine_songlists;

    private RelativeLayout mine_recently_play;//最近播放
    private Intent intent = new Intent();

    private RelativeLayout mine_music_download;//歌曲下载

    private LinearLayout mine_collection_linear;//我的最爱

    private LinearLayout mine_songlist_linear;//我收藏的歌单

    private TextView mine_recently_count;//最近播放歌曲数目

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, null);
        image_bg = (ImageView) view.findViewById(R.id.image_bg);
        headpicture = (CircleImageView) view.findViewById(R.id.headpicture);
        headpicture.setOnClickListener(this);
        mine_login = (TextView) view.findViewById(R.id.mine_login);
        mine_login.setOnClickListener(this);

        personalScrollView = (PersonalScrollView) view.findViewById(R.id.personalScrollView);
        line = view.findViewById(R.id.line);
//        personalScrollView.setTurnListener(this);
        // 设置背景图片
        personalScrollView.setImageView(image_bg);
        personalScrollView.setLine(line);

        // 扫描本地音乐
        mine_local_music = (RelativeLayout) view.findViewById(R.id.mine_local_music);
        mine_local_music.setOnClickListener(this);

        mine_local_songs = (TextView) view.findViewById(R.id.mine_local_songs);

        //最近播放
        mine_recently_play = (RelativeLayout) view.findViewById(R.id.mine_recently_play);
        mine_recently_play.setOnClickListener(this);

        mine_recently_count = (TextView) view.findViewById(R.id.mine_recently_count);

        //歌曲下载
        mine_music_download = (RelativeLayout) view.findViewById(R.id.mine_music_download);
        mine_music_download.setOnClickListener(this);

        //我的最爱
        mine_collection_linear = (LinearLayout) view.findViewById(R.id.mine_collection_linear);
        mine_collection_linear.setOnClickListener(this);

        mine_collections = (TextView) view.findViewById(R.id.mine_collections);
        mine_songlists = (TextView) view.findViewById(R.id.mine_songlists);

        mine_songlist_linear = (LinearLayout) view.findViewById(R.id.mine_songlist_linear);
        mine_songlist_linear.setOnClickListener(this);

        String icon = PreferencesUtils.getString(mContext, "profile_image_url", "");
        String name = PreferencesUtils.getString(mContext, "screen_name", "");
        if (icon != "" && name != "") {
            Picasso.with(mContext).load(icon).placeholder(R.mipmap.default_user_hole).error(R.mipmap.default_user_hole).into(headpicture);
            mine_login.setText(name);
        } else {
            headpicture.setBackgroundResource(R.mipmap.default_user_hole);
            mine_login.setText("点击登录");
        }
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        int localmusic = MyConfig.getSharePreInt(mContext, "localMusic", "count");
        mine_local_songs.setText(localmusic + "首");
        int collection = MyConfig.getSharePreInt(mContext, "collection", "count");
        mine_collections.setText(collection + "首");
        int csonglist = MyConfig.getSharePreInt(mContext, "csonglist", "count");
        mine_songlists.setText(csonglist + "个");
        int recently_songs = MyConfig.getSharePreInt(mContext, "recentlyPlay", "count");
        mine_recently_count.setText(recently_songs + "首");

        String icon = PreferencesUtils.getString(mContext, "profile_image_url", "");
        String name = PreferencesUtils.getString(mContext, "screen_name", "");
        if (icon != "" && name != "") {
            Picasso.with(mContext).load(icon).placeholder(R.mipmap.default_user_hole).error(R.mipmap.default_user_hole).into(headpicture);
            mine_login.setText(name);
        } else {
            headpicture.setBackgroundResource(R.mipmap.default_user_hole);
            mine_login.setText("点击登录");
        }

    }

//    @Override
//    public void onTurn() {
//        RotateAnimation animation = new RotateAnimation();
//        animation.setFillAfter(true);
//        animation.setInterpolatedTimeListener(this);
//        headpicture.startAnimation(animation);
//
//        current_id = current_id < mipmap_id.length - 1 ? ++current_id : 0;
//    }

//    @Override
//    public void interpolatedTime(float interpolatedTime) {
//        // 监听到翻转进度过半时，更新图片内容，
//        if (interpolatedTime > 0.5f) {
//            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
//                    mipmap_id[current_id]);
//            headpicture.setImageBitmap(bitmap);
//        }
//
//    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.headpicture:
                intent.setClass(mContext, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.mine_login:
                intent.setClass(mContext, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.mine_local_music:
                intent.setClass(mContext, LocalMusicActivity.class);
                intent.putExtra("resource", "本地音乐");
                startActivity(intent);
                break;
            case R.id.mine_recently_play:
                intent.setClass(mContext, LocalMusicActivity.class);
                intent.putExtra("resource", "最近播放");
                startActivity(intent);
                break;
            case R.id.mine_music_download:
                intent.setClass(getActivity(), MusicDownloadActivity.class);
                startActivity(intent);
                break;
            case R.id.mine_collection_linear:
                intent.setClass(mContext, LocalMusicActivity.class);
                intent.putExtra("resource", "我的最爱");
                startActivity(intent);
                break;
            case R.id.mine_songlist_linear:
                intent.setClass(mContext, CollectionSongListActivity.class);
                startActivity(intent);
                break;
        }
    }

}
